package com.wenhai.spring.aop.pointcut;

import org.springframework.aop.Advisor;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.aop.support.DefaultPointcutAdvisor;
import org.springframework.aop.support.NameMatchMethodPointcut;

/**
 *
 *
 * @author wenhai
 * @date   2021/5/6
 * @see org.springframework.aop.support.NameMatchMethodPointcut
 */
public class NamePointcutDemo {
    public static void main(String[] args) {
        GrammyGuitarist grammyGuitarist = new GrammyGuitarist();

        NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();

        pointcut.addMethodName("sing").addMethodName("rest");
        Advisor advisor = new DefaultPointcutAdvisor(pointcut, new SimpleAdvice());
        ProxyFactory proxyFactory = new ProxyFactory();
        proxyFactory.setTarget(grammyGuitarist);
        proxyFactory.addAdvisor(advisor);
        GrammyGuitarist proxy = (GrammyGuitarist) proxyFactory.getProxy();
        proxy.rest();
        proxy.talk();
        proxy.sing(new Guitar());
        proxy.sing("wenhai");
    }
}
